package com.homwee.listMenu.util.view;

import android.content.Context;


import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.homwee.factory.R;
import com.homwee.listMenu.util.MenuItemData;
import com.homwee.listMenu.util.MenuItemView;

public class ItemOptionView extends MenuItemView{
	RelativeLayout mImage;
	TextView mName;
	TextView mValue;
	TextView mDotL;
	TextView mDotM;
	TextView mDotR;
	RelativeLayout layout;
	public ItemOptionView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	public ItemOptionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public ItemOptionView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		layout =(RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.menu_optionview, null);
		this.addView(layout);
		mImage =(RelativeLayout) layout.findViewById(R.id.ItemImage);
		mName =(TextView) layout.findViewById(R.id.ItemName);
		mValue =(TextView) layout.findViewById(R.id.Value);
		mDotL = (TextView) layout.findViewById(R.id.DotL);
		mDotM = (TextView) layout.findViewById(R.id.DotM);
		mDotR = (TextView) layout.findViewById(R.id.DotR);
		mDotL.setTextColor(Color.GRAY);
		mDotM.setTextColor(Color.WHITE);
		mDotR.setTextColor(Color.GRAY);
		mMenuData.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","OptionView valueChange");
				if(mMenuData.mOptionValue !=null){
					mValue.setText(mMenuData.mOptionValue[mMenuData.mInitValue]);
					initDot(mMenuData.mInitValue ,mMenuData.mOptionValue.length);
				}else{
					mValue.setText(mMenuData.mInitValue +"");
					//initDot(mMenuData.mInitValue -mMenuData.mMinValue ,mMenuData.mMaxValue -mMenuData.mMinValue+1);
					initDot(mMenuData.mInitValue-1,mMenuData.mMaxValue);
				}
			}
		});
	}
	@Override
	public void update() {
		// TODO Auto-generated method stub
		if(mMenuData.isEnable){
			mName.setTextColor(Color.WHITE);
			mValue.setTextColor(Color.WHITE);
			mDotM.setTextColor(Color.WHITE);
			//mDotM.setTextSize(42);
		}else{
			mDotM.setTextColor(Color.GRAY);
			mName.setTextColor(Color.GRAY);
			mValue.setTextColor(Color.GRAY);
			//mDotM.setTextSize(40);
		}
		mImage.setBackgroundResource(mMenuData.mNameImage);
		mName.setText(mMenuData.mName);
		if(mMenuData.mInitValue >mMenuData.mMaxValue){
			mMenuData.mInitValue =mMenuData.mMaxValue;
		}else if(mMenuData.mInitValue <mMenuData.mMinValue){
			mMenuData.mInitValue =mMenuData.mMinValue;
		}
		if(mMenuData.mOptionValue !=null){
			mValue.setText(mMenuData.mOptionValue[mMenuData.mInitValue]);
			initDot(mMenuData.mInitValue ,mMenuData.mOptionValue.length);
		}else{
			mValue.setText(mMenuData.mInitValue +"");
			//initDot(mMenuData.mInitValue -mMenuData.mMinValue ,mMenuData.mMaxValue -mMenuData.mMinValue+1);
			initDot(mMenuData.mInitValue-1 ,mMenuData.mMaxValue);
		}
		
		
	}
	private void initDot(int curIndex,int dotCount){
		String DotL = "";
		String DotR = "";;
		for(int i=0;i<curIndex;i++){
			DotL =DotL+".";
		}
		for(int i=curIndex+1;i<dotCount;i++){
			DotR =DotR+".";
		}
		if(dotCount>8){
			mDotL.setText(curIndex+1+"");
			mDotM.setText("/");
			mDotR.setText(dotCount+"");
			mDotL.setTextSize(12);
			mDotM.setTextSize(12);
			mDotR.setTextSize(12);
			mDotL.setTextColor(Color.WHITE);
			mDotM.setTextColor(Color.WHITE);
			mDotR.setTextColor(Color.WHITE);
		}else{
			mDotL.setText(DotL);
			mDotM.setText(".");
			mDotR.setText(DotR);
		}
	}
}